using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CreateFontTest
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private IntPtr hDC = IntPtr.Zero;

		private void EnsureHandleExists()
		{
			if (hDC == IntPtr.Zero)
			{
				IntPtr hWnd = this.pictureBox1.Handle;
				hDC = API.GetDC(hWnd);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			UpdateDisplay();
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			UpdateDisplay();
		}

		private void UpdateDisplay()
		{
			EnsureHandleExists();
			DrawString(hDC, "VB.NET", true);
			DrawString(hDC, "C#", false);
		}

		public void DrawString(IntPtr hDC, string Text, bool UseVBDeclaration)
		{
			IntPtr hFont;

				if (UseVBDeclaration)
				{
					hFont = CreateFontVB();
				}
				else
				{
					hFont = CreateFontCSharp();
				}
			 
				IntPtr hOldFont = API.SelectObject(hDC, hFont);

					API.RECT r = new API.RECT();

					r.Left = 0;
					r.Top = 0;
					r.Right = 500;
					r.Bottom = 500;

					API.ExtTextOut(hDC, r.Left, r.Top, 4, ref r, Text, (uint)Text.Length, null);

				API.SelectObject(hDC, hOldFont);
			API.DeleteObject(hFont);
		}

		private IntPtr CreateFontCSharp()
		{
			return API.CreateFont(64, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 2, 0, "Verdana");
		}

		private IntPtr CreateFontVB()
		{
			string FontName = "Courier New";
			return (IntPtr) global::Win32API.Functions.CreateFont(64, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 2, 0, ref FontName);
		}
	}
}